package com.GDOU.controller.activity;

import com.GDOU.bean.activity.Activity;
import com.GDOU.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping("/save")
    public Result save(@RequestBody Activity activity) {
        boolean flag = activityService.save(activity);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Activity activity) {
        boolean flag = activityService.update(activity);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/delete")
    public Result delete(@PathVariable Integer id) {
        boolean flag = activityService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/getById")
    public Result getById(@PathVariable Integer id) {
        Activity activity = activityService.getById(id);
        Integer code = activity != null ? Code.GET_OK : Code.GET_ERR;
        String msg = activity != null ? "" : "数据查询失败，请重试！";
        return new Result(code,activity,msg);
    }

    @GetMapping("/getAll")
    public Result getAll() {
        List<Activity> bookList = activityService.getAll();
        Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = bookList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,bookList,msg);
    }
}
